<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
import {iframe, in_right_admin} from "@/js/tool.js";
import list_url from "@/components/list_url.vue";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";
import {_config, decode_config} from "@/js/s94_tool.js";

export default {
  components: {list_url},
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if (is_del){
      if(in_right_admin('Admin/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin('Admin/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      if(in_right_admin('Admin/add')) btns['添加'] = function(){ iframe('admin/edit') }
      if(in_right_admin('Admin/del')) btns['删除'] = function(){ app.$refs['list_url'].del() }
      if(in_right_admin('Admin/recycle')) btns['回收站'] = function(){ iframe('admin/index?is_del=1') }
    }
    let forms_config = {
      create_time: {label:'关键词',name:'where[name|phone|nickname][like]',config:'Y-M-D',type:'text_one',value:''},
      admin_role_ids: {label:'角色',name:'where[admin_role_ids][like]',type:'select_one',config:[],value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      head_img: {name:'head_img', label:'头像', width:'60px', type:'file_img'},
      name: {name:'name', label:'用户名', width: '120px', order:true},
      phone: {name:'phone', label:'电话号码', width:'120px'},
      nickname: {name:'nickname', label:'昵称', width: '120px'},
      truename: {name:'truename', label:'真实姓名', width: '120px'},
      admin_role_ids: {name:'admin_role_ids', label:'角色', min_width: '120px', type:'select_more', config:[]},
      status: {name:'status',label:'状态',type:'radio_switch',config:["禁用","启用"], width: '80px'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/admin/'+(is_del?'recycle':'list'),
        title: "管理员列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
        order: 'id desc'
      },
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('Admin/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('admin/edit?'+s94Mjs.param_encode(param))
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    ajax({url:'/admin/admin_role/list?order[id]=asc',cache:true}, (res)=>{
      let role_ids_config = [];
      res.data.list.forEach(function(row){
        role_ids_config.push({
          content: row.name,
          value: ','+row.id+',',
        })
      })
      app.config.forms_config.admin_role_ids.config = role_ids_config;
      app.config.list_cols.admin_role_ids.config = decode_config(res.data.list, {content_key:'name', value_key:'id'});
      return false;
    })
  }
}
</script>
